#!/usr/bin/env python
# -*- coding: utf-8 -*-

import multiprocessing
from time import sleep
from random import random

def consumer(q):
    while True:
        item = q.get()
        #处理队列中的项
        print("Handling: " + str(item))
        sleep(random()*5)
        q.task_done()   #处理完队列中的一项任务，发出信号通知任务完成

def producer(seq, q):
    for item in seq:
        q.put(item)
        print("produce: " + str(item))

if __name__ == '__main__':
    q = multiprocessing.JoinableQueue()
    consumer1 = multiprocessing.Process(target=consumer, args=(q,))
    consumer2 = multiprocessing.Process(target=consumer, args=(q,))
    consumer1.daemon = True     #主进程结束时同时结束子进程
    consumer2.daemon = True
    consumer1.start()
    consumer2.start()
    seq = [1,2,3,4,5]
    producer(seq, q)
    q.join() #等待队列中所有项被处理完成（通过接收task_done返回的信号来确定处理完成）